package leetcode.view0105;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public boolean oneEditAway(String first, String second) {
		int len1 = first.length();
		int len2 = second.length();
		if (Math.abs(len1 - len2) > 1) {
			return false;
		}
		if (len1 + len2 == 1) {
			return true;
		}
		char[] fs = first.toCharArray();
		char[] ss = second.toCharArray();
		if (len1 == len2) {
			int diff = 0;
			for (int i = 0; i < ss.length; i++) {
				if (fs[i] != ss[i]) {
					diff++;
					if (diff > 1) {
						return false;
					}
				}
			}
			return true;
		} else {
			if (len2 > len1) {
				return this.del(ss, fs, len1);
			} else {
				return this.del(fs, ss, len2);
			}
		}
	}

	private boolean del(char[] ss, char[] fs, int len) {
		int diff = 0;
		int l = 0, s = 0;
		while (s < len) {
			if (fs[s] != ss[l]) {
				diff++;
				if (diff > 1) {
					return false;
				}
			} else {
				s++;
			}
			l++;
		}
		return true;
	}

	@Override
	public void debug4() {
		H.compare(true, this.oneEditAway("a", "cb"));
		H.compare(true, this.oneEditAway("a", "ab"));
		H.compare(true, this.oneEditAway("a", ""));
		H.compare(true, this.oneEditAway("pale", "ple"));
		H.compare(false, this.oneEditAway("pales", "pal"));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
